package com.darcytech.debezium.core.api;

import com.darcytech.debezium.common.utils.ObjectMapperUtils;
import com.darcytech.debezium.core.api.bo.RegisterInfo;
import com.darcytech.debezium.core.api.bo.RegisterResult;
import com.darcytech.debezium.core.rpc.proto.Rpc;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.UUID;

public interface Api {

    RegisterResult registerConnector(RegisterInfo registerInfo);

    Rpc.Resp PING = Rpc.Resp.newBuilder()
            .setRequestId(UUID.randomUUID().toString())
            .setRequestType(Api.Binding.PING.getType())
            .setData(ObjectMapperUtils.writeAsString(Ping.getInstance()))
            .build();


    enum Binding {
        REGISTER("register", RegisterInfo.class),
        PING("ping", Ping.class),
        ;

        private final String type;

        private final Class<?> requestClazz;

        public String getType() {
            return type;
        }

        public Class<?> getRequestClazz() {
            return requestClazz;
        }


        Binding(String type, Class<?> requestClass) {
            this.type = type;
            this.requestClazz = requestClass;
        }

        private static final ImmutableMap<String, Binding> REQUEST_CLAZZ_MAP = Maps.uniqueIndex(Lists.newArrayList(Binding.values()), Binding::getType);

        public static Class<?> getRequestClazzByType(String type) {
            return REQUEST_CLAZZ_MAP.get(type).getRequestClazz();
        }


    }

}
